package com.boot.main.config.configDemo;
import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//过滤器
public class ReqFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException{
        System.out.println("ReqFilter init");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
        System.out.print("ReqFilter doFilter, url is:");
        String url = ((HttpServletRequest)servletRequest).getServletPath();
//        System.out.println(url);
        if(url.indexOf("hacker") == -1){
            filterChain.doFilter(servletRequest, servletResponse);
            // filterChain.doFilter方法把请求传递下去，往下则由其他过滤器继续处理请求
        } else {
            System.out.print("this url is filtered");
        }
    }
    @Override
    public void destroy(){
        System.out.println("ReqFilter destory");
    }
}
